/*
 * @Author: zhanggongze
 * @Date: 2021-12-09 14:46:02
 * @LastEditors: zgz
 * @LastEditTime: 2022-02-25 09:58:04
 * @FilePath: \taocan_pdf\src\app\reportpdf\reportpdf.component.ts
 * @Description: 
 * Copyright (c) 2022 by 汉吉健康, All Rights Reserved. 
 */
import { HttpClient } from '@angular/common/http';
import { Component, OnInit, ViewChild } from '@angular/core';

import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { CoolLocalStorage } from '../common/services/coolStorage/cool-local-storage';
import { CoolSessionStorage } from '../common/services/coolStorage/cool-session-storage';
import { HttpClientService } from '../common/services/httpClient/http';
import { ReportpdfService } from './service/reportpdf.service';
import * as XLSX from 'xlsx';

@Component({
  selector: 'app-reportpdf',
  templateUrl: './reportpdf.component.html',
  styleUrls: ['./reportpdf.component.scss']
})

export class ReportpdfComponent implements OnInit {

  listOfData = [
    {
      key: '1',
      name: 'John Brown',
      age: 32,
      tel: '0571-22098909',
      phone: 18889898989,
      address: 'New York No. 1 Lake Park'
    },
    {
      key: '2',
      name: 'Jim Green',
      tel: '0571-22098333',
      phone: 18889898888,
      age: 42,
      address: 'London No. 1 Lake Park'
    },
    {
      key: '3',
      name: 'Joe Black',
      age: 32,
      tel: '0575-22098909',
      phone: 18900010002,
      address: 'Sidney No. 1 Lake Park'
    },
    {
      key: '4',
      name: 'Jim Red',
      age: 18,
      tel: '0575-22098909',
      phone: 18900010002,
      address: 'London No. 2 Lake Park'
    },
    {
      key: '5',
      name: 'Jake White',
      age: 18,
      tel: '0575-22098909',
      phone: 18900010002,
      address: 'Dublin No. 2 Lake Park'
    }
  ];

  _list: any = []

  inputdata: any = []

  constructor(
    public router: Router,
    private service: ReportpdfService,
    private sessionStorage: CoolSessionStorage,
    private message: NzMessageService,
    private modalService: NzModalService,
    private httpClient: HttpClientService,
    private http: HttpClient,
    private localStorage: CoolLocalStorage,

  ) { }

  ngOnInit() {

  }

  // 导入
  daoru(evt: any) {
    
    /* wire up file reader */
    const target: DataTransfer = <DataTransfer>(evt.target);
    if (target.files.length !== 1) throw new Error('Cannot use multiple files');
    const reader: FileReader = new FileReader();
    reader.onload = (e: any) => {
      /* read workbook */
      const bstr: string = e.target.result;
      const wb: XLSX.WorkBook = XLSX.read(bstr, { type: 'binary' });
      this.inputdata = [];
      wb.SheetNames.forEach((e_name: any) => {
        let o: any = {};
        o.name = e_name;
        console.log(e_name);
        let _data = XLSX.utils.sheet_to_json(wb.Sheets[e_name], { header: 1 })

        let _items: any = [];
        _data.forEach((e_item: any) => {
          if (e_item[0] !== '检查名称') {
            _items.push({
              check_name: e_item[0],
              check_price: e_item[1],
              check_introduce: e_item[2],
            });
          }
        });

        o.items = _items;
        if (_items.length != 0) {
          this.inputdata.push({
            name: e_name,
            items: _items,
            rowspan: _items.length + 1
          });
        }
      });

      console.log(this.inputdata)

      /* grab first sheet */
      // const wsname: string = wb.SheetNames[0];
      // const ws: XLSX.WorkSheet = wb.Sheets[wsname];

      // /* save data */
      // this.inputdata = (XLSX.utils.sheet_to_json(ws, {header: 1}));



      // console.log(this.inputdata)

      evt.target.value = "" // 清空
    };
    reader.readAsBinaryString(target.files[0]);

  }

}

